<?php
	/**
	 * 用户登录注册入口
	 */
	class UserEntryAction extends Action{

		/**
		 * [userReg 用户注册页面]
		 * @return [type] [description]
		 */
		public function userReg(){

			$this->display();
		}

		/**
		 * [addUser 添加新用户]
		 */
		public function addUser(){
			if($this->isPost()){
				if(isset($_POST['email'])){
				$userRegData = array(
					'username'=>$this->_post('email'),
					'password'=>md5($this->_post('password1')),
					'email'=>$this->_post('email')
				);
				}else if(isset($_POST['phone'])){
					$userRegData = array(
						'username'=>$this->_post('phone'),
						'password'=>md5($this->_post('password1')),
						'moblie'=>$this->_post('phone')
					);
				}
				$db = M("user");
				if($db->add($userRegData)){
					$_SESSION['tmpuname'] = $userRegData['username'];
					$this->redirect("welcome");
					
				}
			}else{
				exit('what do you want to do?');
			}
		}

		/**
		 * 注册成功后欢迎页
		 * @return [type] [description]
		 */
		public function welcome(){
			$this->username = $_SESSION['tmpuname'];
			$this->display();
		}

		/**
		 * 用户登录界面
		 * @return [type] [description]
		 */
		public function login(){
			if($this->isAjax()){
				$_SESSION['from'] = $this->_post('from');
				echo json_encode(array("from"=>$_SESSION['from']));
				die;
			}
			if(isset($_SESSION['from'])){
				$this->pageFrom = $_SESSION['from'];
			}else{
				$this->pageFrom = '';
			}
			$this->display();
		}

		/**
		 * 用户登录信息异步验证
		 * @return [type] [description]
		 */
		public function userCheck(){
			if($this->isAjax()){
				//验证用户名
				if(!isset($_POST['userpasswd']) && isset($_POST['username'])){
					$userName = $this->_post('username');
					$db = M("user");
					$result = $db->where(array('username'=>$userName))->find();
					if(is_array($result) && $result['username'] == $userName){
						echo json_encode(array('status'=>'ok'));
					}else{
						echo json_encode(array('status'=>'er'));
					}
				}
				//验证用户密码
				if(isset($_POST['userpasswd']) && isset($_POST['username'])){
					$userName = $this->_post('username');
					$db = M("user");
					$result = $db->where(array('username'=>$userName))->find();
					if(is_array($result) && $result['username'] == $userName){
						$userpasswd = $this->_post('userpasswd');
						if($result['password'] == md5($userpasswd)){
							echo json_encode(array('status'=>'ok'));
							$_SESSION['userid'] = $result['id'];
							$_SESSION['username'] = $result['username'];
						}else{
							echo json_encode(array('status'=>'pser'));
						}
						
					}else{
						echo json_encode(array('status'=>'er'));
					}
				}
			}	
		}

		/**
		 * 用户登录成功提示
		 * @return [type] [description]
		 */
		public function loginSuccess(){
			$this->display();
		}

		/**
		 * 用户退出登录
		 * @return [type] [description]
		 */
		public function logout(){
			session_unset();
			session_destroy();
			$this->display();
		}
	}
?>